package com.example.geoquiz;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class QuizActivity extends Activity {
	private Button mTrueButton;
	private Button mFalseButton;
	private Button mNextButton;
	private TextView mQuestionTextView;
	private Button mCheatButton;
	private boolean mIsCheater;
	
	private static final String TAG = "QuizActivity";
	private static final String KEY_INDEX = "index";
	private static final String KEY_IS_CHEATER = "is_cheater";
	
	private TrueFalse[]  mQuestionBank = new TrueFalse[]{
			new TrueFalse(R.string.question_one, true, false),
			new TrueFalse(R.string.question_two, true, false),
			new TrueFalse(R.string.question_three, false, false),
			new TrueFalse(R.string.question_four, false, false)
	};

	private int mCurrentIndex = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(savedInstanceState != null){
//			boolean isCheater = savedInstanceState.getBoolean(KEY_IS_CHEATER);
			mCurrentIndex = savedInstanceState.getInt(KEY_INDEX);
			mQuestionBank = (TrueFalse[]) savedInstanceState.getParcelableArray(KEY_IS_CHEATER);
//			mIsCheater = savedInstanceState.getBoolean(KEY_IS_CHEATER);
//			mQuestionBank[mCurrentIndex].setIsCheater(isCheater);
//			mIsCheater = mQuestionBank[mCurrentIndex].isIsCheater();
		}
		setContentView(R.layout.activity_quiz);
		mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
		updateQuestion();
		
		mTrueButton = (Button)findViewById(R.id.true_btn);
		mTrueButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Toast.makeText(QuizActivity.this, R.string.correct_string	, Toast.LENGTH_SHORT).show();;
				checkAnswer(true);
			}
		});
		mFalseButton = (Button)findViewById(R.id.false_btn);
		mFalseButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				Toast.makeText(QuizActivity.this, R.string.error_string, Toast.LENGTH_SHORT).show();
				checkAnswer(false);
			}
		});
		
		mNextButton = (Button)findViewById(R.id.next_btn);
		mNextButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
				updateQuestion();
				
				mIsCheater = false;
			}
		});
		
		mCheatButton = (Button)findViewById(R.id.cheat_button);
		mCheatButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(QuizActivity.this, CheatActivity.class);
				boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
				i.putExtra(CheatActivity.EXTRA_ANSER_IS_TRUE, answerIsTrue);
//				startActivity(i);
				startActivityForResult(i, 0);
			}
		});
	}

	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.d(TAG, "onPause()");
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(TAG, "onResume()");
	}


	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d(TAG, "nStart()");
	}


	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.d(TAG, "onStop()");
	}


	private void updateQuestion(){
		
		int question = mQuestionBank[mCurrentIndex].getQuestion();
		mQuestionTextView.setText(question);
	}
	
	private void checkAnswer(boolean userPressTrue){
		boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
		boolean isCheater = mQuestionBank[mCurrentIndex].isIsCheater();
		int msgId;
		if (isCheater) {
			msgId = R.string.judgment_toast;
		} else {
			if (userPressTrue == answerIsTrue) {
				msgId = R.string.correct_string;
			} else {
				msgId = R.string.error_string;
			}
		}
		Toast.makeText(this, msgId, Toast.LENGTH_SHORT).show();
	}


	@Override
	protected void onSaveInstanceState(Bundle saveInstanceState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(saveInstanceState);
		Log.d(TAG, "onSaveInstanceState");
		saveInstanceState.putInt(KEY_INDEX, mCurrentIndex);
		saveInstanceState.putParcelableArray(KEY_IS_CHEATER, mQuestionBank);
//		saveInstanceState.putBoolean(KEY_IS_CHEATER, mIsCheater);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		
		if(data  == null){
			return ;
		}
		boolean isCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
//		mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
		mQuestionBank[mCurrentIndex].setIsCheater(isCheater);
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	

	
}
